<?php 
/**
 * Qiaiqia 分页类
 * ============================================================================
 * * 版权所有 2014- 广州维赛网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.weisai.net；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 
 * $Author: hungry $
 * $Id: Page.class.php 17217 2014Z hungry $
 */

Class Page
{
	private $long=10;//分页总长度
	private $page_count;
	private $page_arr;//分页数组
	private $param_name='page';//参数名称，用来获取当前页数
	private $lang=array('first_page'=>'首页','next_page'=>'下一页','pre_page'=>'上一页','last_page'=>'末页',);
	private $pre_page;
	private $is_ajax;//url传输数据还是ajax传输数据
	private $next_page;
	private $cfg;//配置 only_pn:只显示上下页
	public $select_limit;
	public $select_from;
	
	function __construct($page=1,$record_nums='',$page_size=10,$cfg=array())
	{
		$page=$page<0?1:$page;
		$this->select_limit = $page_size>0?$page_size:10;
		$this->page = $page;
		$this->cfg=$cfg;
   	    $this->select_from = ($page - 1) * $page_size. ',';
   	    $this->page_count = ceil($record_nums/$page_size);
   	    $this->pre_page = ($page == 1)? 1 : $page - 1;
   	    $this->next_page = ($page == $this->page_count)? $this->page_count : $page + 1 ;
		
		
		
		
	}
	/*
	 * 切换语言
	 * */
	public function set_lang($arr)
	{
		$this->lang=$arr;
	
	}
	public function create_arr()
	{
		
		$arr=array();
		$half=ceil($this->long/2);
		$url=$_SERVER['REQUEST_URI'];
		
		if($this->cfg['rewrite']==1)
		{
			$url=isset($_SERVER['HTTP_X_REWRITE_URL'])?$_SERVER['HTTP_X_REWRITE_URL']:$_SERVER['REQUEST_URI'];
		$query=strpos($url, '?')?substr($url, strpos($url, '?')):'';
		  $url=substr($url,0, strlen($url)-strlen($query));
		
		$url=(preg_replace('/\_([0-9]+\.html$)|(.html)/', '', $url));
		$first_page=array('url'=>$url."_1.html".$query,'txt'=>$this->lang['first_page'],'class'=>'hy_first_page');
		$last_page=array('url'=>$url.'_'.$this->page_count.'.html'.$query,'txt'=>$this->lang['last_page'],'class'=>'hy_last_page');
		$pre_page =array('url'=>$url.'_'.$this->pre_page.'.html'.$query ,'txt'=>$this->lang['pre_page'],'class'=>'hy_pre_page');
		$next_page=array('url'=>$url.'_'.$this->next_page.'.html'.$query ,'txt'=>$this->lang['next_page'],'class'=>'hy_next_page');
		}
		else 
		{
		$url=(preg_replace('(\&page=[0-9]+)', '', $url));
		$first_page=array('url'=>$url.'&'.$this->param_name."=1",'txt'=>$this->lang['first_page'],'class'=>'hy_first_page');
		$last_page=array('url'=>$url.'&'.$this->param_name."=".$this->page_count,'txt'=>$this->lang['last_page'],'class'=>'hy_last_page');
		$pre_page =array('url'=>$url.'&'.$this->param_name."=".$this->pre_page ,'txt'=>$this->lang['pre_page'],'class'=>'hy_pre_page');
		$next_page=array('url'=>$url.'&'.$this->param_name."=".$this->next_page ,'txt'=>$this->lang['next_page'],'class'=>'hy_next_page');
		}
		
		if($this->cfg['only_pn']==1)
		{
				return $this->page_arr=array($pre_page,$next_page);
		}
		
		if($this->page_count<$this->long)
		{
			for($i=1;$i<$this->page_count+1;$i++)
   		   {
   		      $arr[$i]['txt']=$i;
   		      if($this->page==$i)
   		      {
   		      	$arr[$i]['url']='javascript:;';
   		      	$arr[$i]['class']='hy_cur_page';
   		      }
   		      else 
   		      {
   		      	if($this->cfg['rewrite']==1)
   		      	{
   		      	 $arr[$i]['url']=$url.'_'.$i.'.html';
   		      	}
   		      	else
   		      	{
   		         $arr[$i]['url']=$url.'&'.$this->param_name.'='.$i;
   		      	}
   		         $arr[$i]['class']='';
   		      }
   		     
   		   }
		}
		else
		{
			
			if($this->page>0&&$this->page<$this->long)
			{
				
				for($i=1;$i<=$this->long;$i++)
				{
					$arr[$i]['txt']=$i;
				   if($this->page==$i)
				  {
				   $arr[$i]['url']='javascript:;';
				   $arr[$i]['class']='hy_cur_page';
				  }
				   else
				  {
				  	if($this->cfg['rewrite']==1)
				  	{
				  		$arr[$i]['url']=$url.'_'.$i.'.html';
				  	}
				  	else
				  	{
					$arr[$i]['url']=$url.'&'.$this->param_name.'='.$i;
				  	}
					$arr[$i]['class']='';
				   }
			
				}
				$arr[]=array('text'=>'...','url'=>'javascript:;');
			}
			elseif($this->page>=$this->long&&$this->page<=$this->page_count-$this->long)
			{
				$arr[]=array('text'=>'...','url'=>'javascript:;');
				for($i=$this->page-$half;$i<$this->page+$half;$i++)
				{
				  $arr[$i]['txt']=$i;
				  if($this->page==$i)
				 {
				
				    $arr[$i]['url']='javascript:;';
				    $arr[$i]['class']='hy_cur_page';
				  }
				   else
				  {
				  	if($this->cfg['rewrite']==1)
				  	{
				  		$arr[$i]['url']=$url.'_'.$i.'.html';
				  	}
				  	else
				  	{
				    $arr[$i]['url']=$url.'&'.$this->param_name.'='.$i;
				  	}
				    $arr[$i]['class']='';
				  }
				}
				$arr[]=array('text'=>'...','url'=>'javascript:;');
			}
            elseif($this->page>$this->page_count-$this->long)
			{
				
					for($i=$this->page_count-$this->long;$i<$this->page_count+1;$i++)
					{
						$arr[$i]['txt']=$i;
					   if($this->page==$i)
					   {
						$arr[$i]['url']='javascript:;';
						$arr[$i]['class']='hy_cur_page';
					    }
						else
						{
							if($this->cfg['rewrite']==1)
							{
								$arr[$i]['url']=$url.'_'.$i.'.html';
							}
							else
							{
						        $arr[$i]['url']=$url.'&'.$this->param_name.'='.$i;
							}
						$arr[$i]['class']='';
						}
					}
					$arr[]=array('text'=>'...','url'=>'javascript:;');
			}			
		  }
		  array_unshift($arr, $first_page, $pre_page);
		  array_push($arr,$next_page,$last_page);
		
		return $this->page_arr=$arr;

	}
	public function html_page()
	{
		
		$html='<div class="hy_page">';
		
		if($this->page_arr)
		{
			
			foreach($this->page_arr as $k=>$v)
			{
				
				$html.="<a class='".$v['class']."' href='".$v['url']."'>".$v['txt']."</a>";
			}
		}
		$html.='</div>';
		
		return $html;
	}
}